<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>出库审核</title>
</head>
<body>
	<script type="text/javascript">

		$(function() {

			//出库请领
			$('#review-dg').datagrid({
				url : 'findall-apply',
				pagination : true,
				pageNumber : 1,
				pageSize : 7,
				pageList : [ 7, 10 ],
				columns : [ [
					{field : 'drugapply_id',			title : '请领单号',		width : 260},
					{field : 'drugapply_people',		title : '请领人',		width : 260},
					{field : 'drugapply_time',			title : '请领时间',		width : 260,formatter:comFormatDate},
					{field : 'drugapply_type',			title : '请领方式',		width : 265,hidden:true},
					{field : 'drugapply_allnumber',		title : '总数量',		width : 268}
				] ],
				onDblClickRow : function(rowIndex, rowData) {
					$("#review-win").window("open");
					//获取请领单号
					$("#review-qldh").textbox('setValue',rowData.drugapply_id);
					//获取请领方式
					if(rowData.drugapply_type==1){
						$("#review-qlfs").textbox('setValue',"药房请领");
					}
					//获取负责人
					$("#review-qlr").textbox('setValue',rowData.drugapply_people);
					
					//请领单号
					var drugapplyid = $("#review-qldh").textbox("getValue");
					
					//给采购详情获取采购单号
					$.ajax({  
                        url: 'findBy-applydet',  
                        data:{'drugapplyid':drugapplyid},
                        type: 'POST',  
                        dataType:"json",
					 	success: function (data) {  
					 				//遍历数据前清空datagrid数据
					 				$('#outreview-dg').datagrid('loadData',{total:0,rows:[]});
					 				//循环订单商品数据
					                for ( var machine in data.result) {
					                    var row_data = {
					    					'drug_number' : data.result[machine].drug_number,
					    					'dp_details_id' : data.result[machine].dp_details_id,
					                        'drug_name' : data.result[machine].drug_name,
					                        'drug_standard' : data.result[machine].drug_standard,
					                        'drug_purchase_price' : data.result[machine].drug_purchase_price,
					                        'inventory_quantity' : data.result[machine].inventory_quantity,
					                        'dp_details_shuliang' : data.result[machine].dp_details_shuliang,
					                        'inventory_number' : data.result[machine].inventory_number,
					                        'dp_details_shijishuliang' : data.result[machine].dp_details_shuliang
					                    };
					                    $('#outreview-dg').datagrid('appendRow', row_data);
					                }
					     }, 				     
                        Error: function () {  
                            alert(Error);  
                        }                   
                    });    
					
				}
			});

			//待出库
			$('#stayoutbound-dg').datagrid({
				url : 'findall-apply',
				pagination : true,
				pageNumber : 1,
				pageSize : 3,
				pageList : [ 3, 6 ],
				columns : [ [
					{field : 'drugapply_id',title : '请领单号',width : 260},
					{field : 'drugapply_people',title : '请领人',width : 260},
					{field : 'drugapply_time',title : '请领时间',width : 260,formatter:comFormatDate},
					{field : 'drugapply_allnumber',title : '总数量',width : 268}
				] ],
				onDblClickRow : function(rowIndex, rowData) {
					$("#stayoutbound-win").window("open");
					//获取请领单号
					$("#review-qldh1").textbox('setValue',rowData.drugapply_id);
					//获取请领方式
					if(rowData.drugapply_type==1){
						$("#review-qlfs1").textbox('setValue',"药房请领");
					}
					//获取负责人
					$("#review-qlr1").textbox('setValue',rowData.drugapply_people);
					
					//请领单号
					var drugapplyid = $("#review-qldh1").textbox("getValue");
					
					//给采购详情获取采购单号
					$.ajax({  
                        url: 'findBy-applydet',  
                        data:{'drugapplyid':drugapplyid},
                        type: 'POST',  
                        dataType:"json",
					 	success: function (data) {  
					 				//遍历数据前清空datagrid数据
					 				$('#stayoutboundxq-dg').datagrid('loadData',{total:0,rows:[]});
					 				//循环订单商品数据
					                for ( var machine in data.result) {
					                    var row_data = {
					    					'drug_number' : data.result[machine].drug_number,
					                        'drug_name' : data.result[machine].drug_name,
					                        'drug_standard' : data.result[machine].drug_standard,
					                        'drug_purchase_price' : data.result[machine].drug_purchase_price,
					                        'inventory_quantity' : data.result[machine].inventory_quantity,
					                        'dp_details_shuliang' : data.result[machine].dp_details_shuliang,				                        
					                        'dp_details_shijishuliang' : data.result[machine].dp_details_shuliang
					                    };
					                    $('#stayoutboundxq-dg').datagrid('appendRow', row_data);
					                }
					     }, 				     
                        Error: function () {  
                            alert(Error);  
                        }                   
                    });    
					
				}
			});

			//请求发药
			$('#outreview-dg').datagrid({
				url : '',
				pagination : true,
				pageNumber : 1,
				pageSize : 3,
				pageList : [ 3, 6 ],
				columns : [ [ 
					{field : 'drug_number',					title : '药品编号',		width : 148.5}, 
					{field : 'dp_details_id',				title : '请领编号',		width : 148.5,hidden:true}, 
					{field : 'drug_name',					title : '药品名称',		width : 148.5}, 
					{field : 'drug_standard',				title : '规格',			width : 148.5}, 
					{field : 'drug_purchase_price',			title : '单价',			width : 148.5}, 
					{field : 'inventory_quantity',			title : '库存数量',		width : 148.5}, 
					{field : 'dp_details_shuliang',			title : '请领数量',		width : 148.5}, 
					{field : 'inventory_number',			title : '库存编号',		width : 149.5,hidden:true} ,
					{field : 'dp_details_shijishuliang',	title : '发货数量',		width : 149.5} 
				] ]
			});
			
			//待发药
			$('#stayoutboundxq-dg').datagrid({
				url : '',
				pagination : true,
				pageNumber : 1,
				pageSize : 3,
				pageList : [ 3, 6 ],
				columns : [ [
					{field : 'drug_number',					title : '药品编号',		width : 145}, 
					{field : 'drug_name',					title : '药品名称',		width : 145}, 
					{field : 'drug_standard',				title : '规格',			width : 125}, 
					{field : 'drug_purchase_price',			title : '单价',			width : 100}, 
					{field : 'inventory_quantity',			title : '库存数量',		width : 110},
					{field : 'dp_details_shuliang',			title : '剩余数量',		width : 110}, 
					{field : 'dp_details_shijishuliang',	title : '发货数量',		width : 110} 
				] ]
			});
			
			// 时间戳
			function CurentTime(){
		  
		        var now = new Date();
		       
		        var year = now.getFullYear();       //年
		        var month = now.getMonth() + 1;     //月
		        var day = now.getDate();            //日
		       
		        var hh = now.getHours();            //时
		        var mm = now.getMinutes();          //分
				var ss = now.getSeconds();			//秒
		       
		        var clock = year + "-";
		       
		        if(month < 10)
		            clock += "0";
		       
		        clock += month + "-";
		       
		        if(day < 10)
		            clock += "0";
		           
		        clock += day + " ";
		       
		        if(hh < 10)
		            clock += "0";
		           
		        clock += hh + ":";
		        if (mm < 10) clock += '0'; 
		        clock += mm + ":"; 
				
		        if (ss < 10) clock += '0'; 
		        clock += ss; 
		        return(clock); 
			}
			
			$("#cancel-outbound").click(function(){
				$("#review-win").window("close");
			});
			//确认出库发药
			$("#save-review1").click(function(){
				$("<form method='Post' id='for'>").appendTo("body").form({
					url:'save-Outbound',
					onSubmit:function(param){
						//获取请领单号
						var applyid = $("#review-qldh").textbox("getValue");
						
						$.ajax({  
	                        url: 'update-apply',  
	                        data:{'applyid':applyid},
	                        type: 'POST',  
	                        dataType:"json",
						 	success: function (data) {  
						
						     }, 				     
	                        Error: function () {  
	                            alert(Error);  
	                        }                   
	                    });    
						
						//出库状态
						param["out.outboundZt"] = 0;
						
						//出库对象
						param["out.outbound_way"] = 0;

						var shijan = CurentTime();
						//获取开始时间，结束时间(--注意：这里要用时间的转换器--)
						param["out.outboundTime"] = shijan;	
						
						//获取负责人
						param["out.outboundPeople"] = $("#outboundPeople").val();		
						
						//获取请领单号
						param["out.outboundApply"] = $("#review-qldh").val();		
						
						//获取表格中的采购的详情记录
						var rows = $("#outreview-dg").datagrid("getRows");
						console.log(rows);

						var total=0;
						var money=0;
						for (var i = 0; i < rows.length; i++) {
							//药品编号
							//param["putdetails["+i+"].inventory_number"] = rows[i]["inventory_number"];
							//单个药品的请领数量                                                          
							param["outdetail["+i+"].outDetailsQuantity"] = rows[i]["dp_details_shuliang"];
							//药品库存编号
							param["outdetail["+i+"].Inventory.inventoryNumber"] = rows[i]["inventory_number"];
							//console.log(param["putdetails["+i+"].inDetailsBatch"]);
							//发货药品的总数量
							total +=rows[i]["dp_details_shijishuliang"];
							//药品的总金额
							//money+=rows[i]["drug_purchase_price"]*rows[i]["in_details_quantity"];
							
							//获取库存号
							var inventorynumber = rows[i]["inventory_number"];						
							//获取发货数量
							var sum = rows[i]["dp_details_shijishuliang"];		
							$.ajax({  
			                       url: 'update-dinvent',  
			                       data:{
			                    	   'inventorynumber':inventorynumber,
			                    	   'sum':sum
			                       },
			                       type: 'POST',  
			                       dataType:"json",
								   success: function (data) {  
								
								   }, 				     
			                       Error: function () {  
			                           alert(Error);  
			                       }                   
			              	});    		
							
							//请领详情编号
							var adetailsid = rows[i]["dp_details_id"];								
							$.ajax({  
			                       url: 'update-applaydis',  
			                       data:{
			                    	   'adetailsid':adetailsid,
			                    	   'sum':sum
			                       },
			                       type: 'POST',  
			                       dataType:"json",
								   success: function (data) {  
								
								   }, 				     
			                       Error: function () {  
			                           alert(Error);  
			                       }                   
			              	});    	
							
						}
						//console.log(total);
						//总数量
						param["out.outboundAllquantity"] = parseInt(total);
						//总金额
						//param["Indent.indentAllmoney"] = money;
					},
					success:function(data){
						 if(parseInt(data)==1){
								$(".drugonea").text("发药成功！！！");
							 	$("#DRUGONEa").window('open');
						    	//三秒之后自动消失
							   	setTimeout(function(){
									$("#DRUGONEa").window('close');
									$('#review-win').window("close");
									$('#review-dg').datagrid("reload");
								}, 2000);
					    	
							    $('#for').form("clear");//清空文本
						}
							
						if(parseInt(data)==2){
						    	$(".drugonea").text("发药失败,稍后重试......");
						 		 //清空文本
						 		$("#DRUGONEa").window('open');
						  	 	//三秒之后自动消失
						   		setTimeout(function(){
						    	 	$("#DRUGONEa").window('close');															    	 	
								}, 2000);
							
						}						
						
					}
				}).submit();
				
			});
			
		});		
			
		// 时间戳
		function comFormatDate(date) {
			var day = new Date(date); 
			var Year = 0; 
			var Month = 0; 
			var Day = 0; 
			var CurrentDate = ""; 
			//初始化时间 
			Year= day.getFullYear();//ie火狐下都可以 
			Month= day.getMonth()+1; 
			Day = day.getDate(); 
			Hour = day.getHours(); 
			 Minute = day.getMinutes(); 
			 Second = day.getSeconds(); 
			CurrentDate += Year + "-"; 
			if (Month >= 10 ) { 
				CurrentDate += Month + "-"; 
			}else { 
				CurrentDate += "0" + Month + "-"; 
			} 
			if (Day >= 10 ) { 
				CurrentDate += Day ; 
			}else { 
				CurrentDate += "0" + Day ; 
			} 
			if(Hour>=10){
				CurrentDate +=" "+Hour;
			}else{
				CurrentDate +=" "+"0"+Hour;
			}
			if(Minute>=10){
				CurrentDate +=":"+Minute;
			}else{
				CurrentDate +=":"+"0"+Minute;
			}
			if(Second>=10){
				CurrentDate +=":"+Second;
			}else{
				CurrentDate +=":"+"0"+Second;
			}
			return CurrentDate; 
		}

	</script>

	<div id="review-p" class="easyui-panel"
		style="width: 100%; height: 100%; padding: 10px; background: #fafafa;"
		data-options="iconCls:'icon-save',closable:true,
	   		 collapsible:true,minimizable:true,maximizable:true">

		<!-- 药品出库窗口 -->
		<fieldset style="border: 1px solid #A6A6A6; border-radius: 6px; width: 1050px; margin: 0 auto; margin-top: 10px; margin-bottom: 20px;height:440px;">
			
			<legend style="font-size: 17px">出库审核</legend>
			
			<table id="review-dg" class="logininform-table easyui-datagrid" style="width: 1030; height: 403px"
				data-options="toolbar:'#logininform-tb',fitColumns:false,singleSelect:false">

			</table>
		
		</fieldset>

	</div>

	<!-- 出库窗口 -->
	<div id="review-win" class="easyui-window" title="请领单" style="width: 1080px; height: 550px"
		data-options="iconCls:'icon-save',modal:true,closed:true,shadow:true,resizable:false,collapsible:false,minimizable:false,maximizable:false">
		
		<div class="easyui-layout" data-options="fit:true">
			
			<div data-options="region:'center'">

				<div style="width: 750px; height: 80px;line-height:80px; margin: 0px auto;">
					
					<p>
						
						<input id="review-qldh" class="easyui-textbox" style="width: 200px" data-options="label:'请领单号：'"></input>
						
						<span style="margin-left: 50px;"></span> 
						
						<input id="review-qlr" class="easyui-textbox" style="width: 200px" data-options="label:'请领人：'"></input>
						
						<span style="margin-left: 50px;"></span> 
						
						<input id="review-qlfs" class="easyui-textbox" style="width: 200px" data-options="label:'请领方式：'"></input>

					</p>

				</div>
				
				<div style="padding: 13px">
					
					<table id="outreview-dg" class="logininform-table easyui-datagrid" style="width: 1040px; height: 308px"
						data-options="toolbar:'#logininform-tb',fitColumns:false,singleSelect:false">

					</table>
				
				</div>
				
				<p style="margin-left: 350px;">
					
					<a id="save-review1" style="width: 100px" class="easyui-linkbutton" iconCls="icon-ok">确认出药</a>
					
					<span style="margin-left: 50px;"></span>
					
					<a id="cancel-outbound" style="width: 100px" class="easyui-linkbutton" iconCls="icon-cancel">取消</a>
				
				</p>
			
			</div>
		
		</div>
	
	</div>

	
	<input type="hidden" value="${user.userName}"  id="outboundPeople"> 
	
	<!-- 弹框提示 -->
	<div id="DRUGONEa" class="easyui-window"  title="提示框" style="width:300px;height:156px"
	    data-options="iconCls:'icon-save',modal:true,resizable:false,collapsible:false,maximizable:false,minimizable:false,closed:true">
	          <p class="drugonea" style=" margin-left: 72px;font-size: 16px;margin-top: 47px;">
	          </p>
	</div>

</body>
</html>